import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _Slide from "@mui/material/Slide";
import _useMediaQuery from "@mui/material/useMediaQuery";
import React, { useCallback, useMemo } from "react";
import PullRefresh from "../../../compoents/pull-refresh";
import classes from "./nfts.module.less";
import { useMount, useReactive } from "ahooks";
import useServices from "../../../hooks/useRequest";
// import { myNft } from "@/api/nft";
import { getNFTAssets } from "../../../api/wallet";
import { EVENT } from "../../../enums";
import NftDetail from "../../nft-detail";
import { TransitionGroup } from "react-transition-group";
import { Power } from "../../../compoents/styled";
import kingsInstance from "../../../instance";
import { useDispatch, useSelector } from "react-redux";
import classNames from "classnames";
import { CHAINMAPTOSERVE, BSC, isDevChain, ETHEREUM, CONFLUX, MAIN_CHAIN, TEST_CHAIN, CHAIN_MAP } from "../../../config";
import { getMapFinalValue, getChainName, matchTheme } from "../../../utils";
import { useLocalStorageSetState } from "../../../hooks";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../../local";
import ChainSwitchNft from "../../../compoents/chain-switch-nft";
import useRouter from "../../../hooks/useRouter";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var params = {
    BSC: BSC,
    ETHEREUM: ETHEREUM,
    CONFLUX: CONFLUX,
    ALL_MAIN: MAIN_CHAIN,
    ALL_TEST: TEST_CHAIN
};
var Nfts = /*#__PURE__*/ React.forwardRef(function(props, ref) {
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), localState = _useLocalStorageSetState[0], setLocalState = _useLocalStorageSetState[1];
    var routerInfo = useRouter();
    var state = useReactive({
        chainType: CHAIN_MAP[localState === null || localState === void 0 ? void 0 : localState.chainNft],
        loading: false,
        list: [],
        // chain: localStore?.chain || 'BSC',
        selectNFT: null
    });
    var dispatch = useDispatch();
    var _useSelector = useSelector(function(rootState) {
        return {
            theme: rootState.app.theme,
            stateChainNft: rootState.app.chainNft
        };
    }), theme = _useSelector.theme, stateChainNft = _useSelector.stateChainNft;
    // const { getNftImgSrc } = useCalculateAmount();
    var services = useServices({
        getNFTAssets: getNFTAssets
    });
    var isPc = _useMediaQuery("(min-width:1280px)");
    var t = useTranslation().t;
    var fetchNFTList = function() {
        var _ref = _async_to_generator(function(chain) {
            var _ref, code, data, list, chainTypeCounts, chainArray, maxChainType, nftChainType, e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        console.log(chain);
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            services.getNFTAssets({
                                pageNum: 1,
                                pageSize: 10000,
                                chainType: chain ? [
                                    CHAIN_MAP[chain][isDevChain ? 1 : 0]
                                ] : isDevChain ? params[state.chainType || "ALL_TEST"].filter(function(c) {
                                    return TEST_CHAIN.includes(c);
                                }) : params[state.chainType || "ALL_MAIN"].filter(function(c) {
                                    return MAIN_CHAIN.includes(c);
                                })
                            })
                        ];
                    case 2:
                        _ref = _state.sent(), code = _ref.code, data = _ref.data;
                        list = code == 200 ? data.list : [];
                        if (!chain) {
                            chainTypeCounts = list.reduce(function(acc, param) {
                                var chainType = param.chainType;
                                acc[chainType] = (acc[chainType] || 0) + 1;
                                return acc;
                            }, {});
                            chainArray = Object.keys(chainTypeCounts);
                            maxChainType = "ETHEREUM";
                            if (chainArray.length > 0) {
                                maxChainType = chainArray.reduce(function(a, b) {
                                    return chainTypeCounts[a] > chainTypeCounts[b] ? a : b;
                                });
                            }
                            nftChainType = CHAINMAPTOSERVE[maxChainType];
                            dispatch.app.SET_CHAIN(nftChainType);
                            changeChain(nftChainType);
                            state.chainType = nftChainType;
                        } else {
                            state.list = list;
                        }
                        return [
                            3,
                            4
                        ];
                    case 3:
                        e = _state.sent();
                        console.log(e);
                        return [
                            3,
                            4
                        ];
                    case 4:
                        return [
                            2
                        ];
                }
            });
        });
        return function fetchNFTList(chain) {
            return _ref.apply(this, arguments);
        };
    }();
    var deleteNFT = useCallback(function(id) {
        state.list = state.list.filter(function(item) {
            return item.id !== id;
        });
    }, [
        state.list
    ]);
    var changeChain = function() {
        var chainType = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
        switch(chainType){
            case "BSC":
                {
                    dispatch.app.SET_CHAIN_NFT("BSC");
                    setLocalState({
                        chainNft: "BSC"
                    });
                    fetchNFTList("BSC");
                    break;
                }
            case "ETHEREUM":
                {
                    dispatch.app.SET_CHAIN_NFT("ETHEREUM");
                    setLocalState({
                        chainNft: "ETHEREUM"
                    });
                    fetchNFTList("ETHEREUM");
                    break;
                }
            case "CONFLUX":
                {
                    dispatch.app.SET_CHAIN_NFT("CONFLUX");
                    setLocalState({
                        chainNft: "CONFLUX"
                    });
                    fetchNFTList("CONFLUX");
                    break;
                }
        }
    };
    useMount(function() {
        var _routerInfo_location, _routerInfo_location_query;
        var _chainNft = routerInfo === null || routerInfo === void 0 ? void 0 : (_routerInfo_location = routerInfo.location) === null || _routerInfo_location === void 0 ? void 0 : (_routerInfo_location_query = _routerInfo_location.query) === null || _routerInfo_location_query === void 0 ? void 0 : _routerInfo_location_query.chainNft;
        if (_chainNft && !localState.chainNft) {
            changeChain(_chainNft);
        } else {
            fetchNFTList(localState.chainNft);
        }
    });
    var filterList = useMemo(function() {
        // return (state.list || []).filter(item =>
        //   CHAIN_MAP[state.chain].includes(item.chainType),
        // );
        return state.list || [];
    }, [
        state.list
    ]);
    var list = useMemo(function() {
        return /*#__PURE__*/ _jsx("div", {
            className: classes.NFT_list,
            children: filterList.map(function(nft) {
                return /*#__PURE__*/ _jsxs("div", {
                    onClick: function() {
                        dispatch.app.PUSH({
                            path: "/nftDetails",
                            query: _object_spread({}, nft)
                        });
                    // state.selectNFT = nft;
                    },
                    className: classes.NFT_item,
                    "data-id": "".concat(nft.type, "-").concat(nft.tokenId),
                    children: [
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.img,
                            children: [
                                /*#__PURE__*/ _jsx("img", {
                                    // src={getNftImgSrc(`${nft.tokenId}`, {
                                    //   chainType: nft.chainType,
                                    //   contract: nft.contract,
                                    // })}
                                    src: nft.logoUrl
                                }),
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.qr,
                                    children: /*#__PURE__*/ _jsx("img", {
                                        src: require("../../../assets/icons/qrTip.svg")
                                    })
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.name,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.name_text,
                                    children: nft.name
                                }),
                                /*#__PURE__*/ _jsx("img", {
                                    className: classes.name_icon,
                                    src: getMapFinalValue([
                                        getChainName(nft.chainType)
                                    ], {
                                        BSC: require("../../../assets/icons/grayBnb.svg"),
                                        ETHEREUM: require("../../../assets/icons/grayEth.svg"),
                                        CONFLUX: require("../../../assets/icons/grayConflux.svg")
                                    })
                                })
                            ]
                        })
                    ]
                }, "".concat(nft.chainType, "-").concat(nft.contract, "-").concat(nft.tokenId, "-").concat(nft.type));
            })
        });
    }, [
        filterList
    ]);
    return /*#__PURE__*/ _jsxs(TransitionGroup, {
        className: classes.animation_wrap,
        children: [
            /*#__PURE__*/ _jsxs("div", _object_spread_props(_object_spread({
                className: classNames(classes.NFT, state.selectNFT ? classes.v_h : ""),
                ref: ref
            }, props), {
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.nav,
                        children: [
                            /*#__PURE__*/ _jsx(ChainSwitchNft, {
                                className: classes.chain,
                                onChainChange: function(nc) {
                                    fetchNFTList(nc);
                                }
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.repo,
                                onClick: function() {
                                    dispatch.app.PUSH({
                                        path: "/nftsHistory",
                                        query: {
                                            from: "NFTS"
                                        }
                                    });
                                },
                                children: /*#__PURE__*/ _jsxs("div", {
                                    className: classes.repo_box,
                                    children: [
                                        /*#__PURE__*/ _jsx("img", {
                                            className: classes.hi,
                                            src: matchTheme(theme, {
                                                light: require("../../../assets/icons/light/repo.svg"),
                                                dark: require("../../../assets/icons/dark/repo.svg")
                                            })
                                        }),
                                        /*#__PURE__*/ _jsx("img", {
                                            src: matchTheme(theme, {
                                                light: require("../../../assets/icons/light/rightPArrow.svg"),
                                                dark: require("../../../assets/icons/dark/rightOLArrow.svg")
                                            })
                                        })
                                    ]
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.NFT_wrap,
                        children: state.list.length > 0 ? !isPc ? /*#__PURE__*/ _jsx(PullRefresh, {
                            onLoadingChange: function(loading) {
                                state.loading = loading;
                            },
                            pullingText: t(LocalKeyMap.pullRefresh),
                            loosingText: t(LocalKeyMap.releaseRefresh),
                            loadingText: "".concat(t(LocalKeyMap.loading), "..."),
                            dependencies: [
                                state.list.length
                            ],
                            onRefresh: fetchNFTList,
                            children: list
                        }) : /*#__PURE__*/ _jsx("div", {
                            style: {
                                height: "100%",
                                overflow: "scroll"
                            },
                            children: list
                        }) : /*#__PURE__*/ _jsxs("div", {
                            className: classes.empty,
                            children: [
                                /*#__PURE__*/ _jsxs("span", {
                                    children: [
                                        " ",
                                        t(LocalKeyMap["nfts.empty"])
                                    ]
                                }),
                                !isPc ? /*#__PURE__*/ _jsx("a", {
                                    onClick: function() {
                                        kingsInstance.emit(EVENT.onMarketPlace);
                                        dispatch.app.SWITCH_VISIBLE(false);
                                    },
                                    children: t(LocalKeyMap["nfts.empty.jump"])
                                }) : ""
                            ]
                        })
                    }),
                    /*#__PURE__*/ _jsx(Power, {
                        children: t(LocalKeyMap.powerByOna)
                    })
                ]
            })),
            state.selectNFT && /*#__PURE__*/ _jsx(_Slide, {
                direction: "left",
                children: /*#__PURE__*/ _jsx("div", {
                    className: classes.NFT_detail,
                    children: /*#__PURE__*/ _jsx(NftDetail, _object_spread_props(_object_spread({}, state.selectNFT), {
                        onBack: function() {
                            state.selectNFT = null;
                        },
                        deleteNFT: deleteNFT
                    }))
                })
            })
        ]
    });
});
export default Nfts;

 //# sourceMappingURL=index.js.map